/**
 * @author ahammer
 * @date 2009 05
 */

/*
 * This an implementation of a JAISTUFF algorithm by R.Santos
 * https://jaistuff.dev.java.net/algorithms.html
 */

/*
 * This file is part of Iqm.
 * Copyright (c) 2010-2011 Helmut Ahammer
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package op;

import jaistuff.TemplateMatchingWithIterators;
import jaistuff.TemplateMatchingWithRasters;

import java.awt.RenderingHints;
import java.awt.image.renderable.ParameterBlock;
import javax.media.jai.ParameterBlockJAI;
import javax.media.jai.PlanarImage;

/**
 * This is the main image processing class
 * There exist two approaches:
 * A user defined JAI operator is just called 
 * or:
 * The actual processing is implemented in this class 
 */
public class IqmTempMatchOperator{ 


  public IqmTempMatchOperator() {
	  //WARNING: Don't declare fields here
	  //Fields declared here aren't thread safe!   
  }
  /**
   * 
   * @param ParametrBlockJAI pb
   */
  public Object run(ParameterBlockJAI pbJAI){
	  Object ob = null;
	  //ob = JAI.create("IqmTempMatch".toLowerCase(), pb, null);  
	  
	  PlanarImage pi1 = (PlanarImage) pbJAI.getSource(0);
	  PlanarImage pi2 = (PlanarImage) pbJAI.getSource(1);
	  int method = pbJAI.getIntParameter(0);
	  String imageName = (String) pi1.getProperty("image_name");
	  
	  //RenderingHints rh = new RenderingHints(JAI.KEY_BORDER_EXTENDER, BorderExtender.createInstance(BorderExtender.BORDER_COPY));	
	  RenderingHints rh = null;	
	  ParameterBlock pb = new ParameterBlock();
	  if (method == 0){ //Raster
		  TemplateMatchingWithRasters tm = new TemplateMatchingWithRasters(pi1, pi2);
		  tm.run();
		  ob = tm.getOutput();
	  }
	  if (method == 01){ //Iterator
		  TemplateMatchingWithIterators tm = new TemplateMatchingWithIterators(pi1, pi2);
		  tm.run();
		  ob = tm.getOutput();
	  }
	  ((PlanarImage)ob).setProperty("image_name", imageName);
	  return ob;
  }
}
